package com.edu.elasticsearch.utils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.elasticsearch.common.geo.GeoPoint;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;

import com.edu.elasticsearch.common.ElasticSearchService;

public class ElasticSearchUtils {
	
	private static Logger logger = Logger.getLogger(ElasticSearchUtils.class);
	
	// 构建索引库（类似数据库中的表）  
	public static XContentBuilder createMapping(String index_name){
		try {
			XContentBuilder builder = XContentFactory.jsonBuilder().startObject()  
			        .startObject(index_name)
			            .startObject("properties")
			                .startObject("id").field("type", "long").endObject()  
			                .startObject("shopCode").field("type", "string").endObject()  
			                .startObject("location").field("type", "geo_point").endObject()  
			                .startObject("startPrice").field("type", "double").endObject()  
			                .startObject("deliveryFee").field("type", "double").endObject()  
			                .startObject("startTime").field("type", "string").endObject()  
			                .startObject("endTime").field("type", "string").endObject()  
			                .startObject("shopName")
			                	.field("type", "text")
				                .field("analyzer", "ik_max_word")
				                .field("search_analyzer", "ik_max_word")
			                .endObject()  
			                .startObject("address").field("type", "string").endObject()  
			                .startObject("imgUrl").field("type", "string").endObject()  
			                .startObject("phoneNumber").field("type", "string").endObject()  
			            .endObject()
			        .endObject()
			    .endObject();
			return builder;
		} catch (IOException e) {
			e.printStackTrace();
			logger.error("创建搜索失败", e);
		}  
		return null;
	}
	
	//类似数据库sql的insert语句
    public static String packageMapping(Map<String,Object> map){
    	 String jsonData = null;  
         try {  
             // 使用XContentBuilder创建json数据  
             XContentBuilder jsonBuild = XContentFactory.jsonBuilder();  
             jsonBuild.startObject().field("id", map.get("id"))  
                     .field("shopName", map.get("shopName"))  
                     //注意纬度在前，经度在后 location类型
                     .startObject("location").field("lat", map.get("lat")).field("lon",map.get("lon")).endObject()
                     .field("shopCode", map.get("shopCode"))  
                     .field("address", map.get("address"))  
                     .field("deliveryFee", map.get("deliveryFee"))  
                     .field("startPrice", map.get("startPrice"))
                     .field("startTime", map.get("startTime"))
                     .field("endTime", map.get("endTime"))
                     .field("imgUrl", map.get("imgUrl"))
                     .field("phoneNumber", map.get("phoneNumber"))
                     .endObject();  
             jsonData = jsonBuild.string();  
             System.out.println(jsonData);  
         } catch (IOException e) {  
             e.printStackTrace();  
         }  
         return jsonData;  
    }
    
    public static void write(int start,int end) throws Exception{
    	List<Map<String,Object>> params = new ArrayList<Map<String,Object>>();
    	for(int i=start;i<=end;i++){ 
    		System.out.println("----------"+i);
    		Map<String,Object> map = new HashMap<String,Object>();
    		map.put("id", i);
    		map.put("shopName", NumericalUtils.getRandomChiness(8)+"（"+i+"）");
    		map.put("location",new GeoPoint(NumericalUtils.nextDouble(30.01111, 39.99999),NumericalUtils.nextDouble(101.01111, 126.99999)));
    		map.put("shopCode", "A1024000100"+i);
    		map.put("address", "深圳市南山区云谷创新产业园2期"+i+"号");
    		map.put("deliveryFee", 4d);
    		map.put("startPrice", 20d);
    		map.put("startTime", "09:00");
    		map.put("endTime", "23:00");
    		map.put("imgUrl", "default.png");
    		map.put("phoneNumber", "15323795872");
    		params.add(map);
    	}
    	int result = ElasticSearchService.insert(params);
    	System.out.println("---------------"+result);
    }
    
    
    public static void main(String[] args) {
    	try {
//    		ElasticSearchService.createIndex();
    		long start = System.currentTimeMillis();
			write(1,500000);
			long end = System.currentTimeMillis();
			System.out.print("===="+(end-start));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
    
}
